<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="Java规范对分布式事务定义了标准的规范Java事务API和Java事务服务，分别是JTA和JTS一个分布式事务必须包括一个事务管理器和多个资源管理器。 资源管理器是任意类型的持久化数据存储；而事务管理器则是承担着所有事务参与单元者的相互通讯的责任。 JTA的规范制定了分布式事务的实现的整套流程框架，定义了各个接口且只有接口，而实现分别交给事务管理器的实现方和资源管理器的实现方">
<meta property="og:type" content="article">
<meta property="og:title" content="spring-boot使用atomikos实现分布式事务">
<meta property="og:url" content="http://littlefxc.github.io/2019/05/06/spring-boot%E4%BD%BF%E7%94%A8atomikos%E5%AE%9E%E7%8E%B0%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="Java规范对分布式事务定义了标准的规范Java事务API和Java事务服务，分别是JTA和JTS一个分布式事务必须包括一个事务管理器和多个资源管理器。 资源管理器是任意类型的持久化数据存储；而事务管理器则是承担着所有事务参与单元者的相互通讯的责任。 JTA的规范制定了分布式事务的实现的整套流程框架，定义了各个接口且只有接口，而实现分别交给事务管理器的实现方和资源管理器的实现方">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20181205164907157.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20181205164937608.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2018120517000116.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2019-05-06T11:42:16.000Z">
<meta property="article:modified_time" content="2021-03-25T13:15:49.032Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="分布式事务">
<meta property="article:tag" content="spring-boot">
<meta property="article:tag" content="atomikos">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20181205164907157.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70">


<link rel="canonical" href="http://littlefxc.github.io/2019/05/06/spring-boot%E4%BD%BF%E7%94%A8atomikos%E5%AE%9E%E7%8E%B0%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>spring-boot使用atomikos实现分布式事务 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%89%8D%E8%A8%80"><span class="nav-text">1.前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%9C%A8SpringBoot%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1"><span class="nav-text">2.在SpringBoot中使用分布式事务</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E7%9B%B2%E7%82%B9%E8%A7%A3%E9%87%8A"><span class="nav-text">2.1.盲点解释</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E7%A1%AE%E4%BF%9Dmysql%E5%BC%80%E5%90%AFXA%E4%BA%8B%E5%8A%A1%E6%94%AF%E6%8C%81"><span class="nav-text">2.2.确保mysql开启XA事务支持</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E9%87%8D%E8%A6%81Maven%E4%BE%9D%E8%B5%96"><span class="nav-text">2.3.重要Maven依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E9%85%8D%E7%BD%AEAtomikos-Druid-MyBatis"><span class="nav-text">2.4.配置Atomikos, Druid, MyBatis</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-1-application-properties"><span class="nav-text">2.4.1.application.properties</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-2-%E9%85%8D%E7%BD%AEAtomikos%E6%95%B0%E6%8D%AE%E6%BA%90%E4%B8%8EMyBatis%E9%9B%86%E6%88%90"><span class="nav-text">2.4.2.配置Atomikos数据源与MyBatis集成</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-3-%E9%85%8D%E7%BD%AE%E5%A3%B0%E6%98%8E%E5%BC%8F%E4%BA%8B%E5%8A%A1-tx-aop"><span class="nav-text">2.4.3.配置声明式事务(tx+aop)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.5.实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1-%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84"><span class="nav-text">2.5.1.项目结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2-%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-text">2.5.2.数据库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-Maven%E4%BE%9D%E8%B5%96"><span class="nav-text">2.5.3.Maven依赖</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-4-%E9%85%8D%E7%BD%AE"><span class="nav-text">2.5.4.配置</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#application-properties"><span class="nav-text">application.properties</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#OneDatabaseConfig"><span class="nav-text">OneDatabaseConfig</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#TwoDatabaseConfig"><span class="nav-text">TwoDatabaseConfig</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#TransactionConfig"><span class="nav-text">TransactionConfig</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-5-%E5%AE%9E%E4%BD%93%E7%B1%BB"><span class="nav-text">2.5.5.实体类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-6-Dao%E5%B1%82"><span class="nav-text">2.5.6.Dao层</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#UserDao"><span class="nav-text">UserDao</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#BookDao"><span class="nav-text">BookDao</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-7-Service%E5%B1%82"><span class="nav-text">2.5.7.Service层</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#Bookservice"><span class="nav-text">Bookservice</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#BookServiceImpl"><span class="nav-text">BookServiceImpl</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-8-Controller%E5%B1%82"><span class="nav-text">2.5.8.Controller层</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-9-%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95"><span class="nav-text">2.5.9.单元测试</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#BookServiceImplTest"><span class="nav-text">BookServiceImplTest</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#BookControllerTest"><span class="nav-text">BookControllerTest</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%BC%95%E7%94%A8"><span class="nav-text">3.引用</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/spring-boot%E4%BD%BF%E7%94%A8atomikos%E5%AE%9E%E7%8E%B0%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          spring-boot使用atomikos实现分布式事务
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 19:42:16" itemprop="dateCreated datePublished" datetime="2019-05-06T19:42:16+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:49" itemprop="dateModified" datetime="2021-03-25T21:15:49+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>Java规范对分布式事务定义了标准的规范Java事务API和Java事务服务，分别是JTA和JTS一个分布式事务必须包括一个事务管理器和多个资源管理器。</p>
<p>资源管理器是任意类型的持久化数据存储；<br>而事务管理器则是承担着所有事务参与单元者的相互通讯的责任。</p>
<p>JTA的规范制定了分布式事务的实现的整套流程框架，定义了各个接口且只有接口，而实现分别交给事务管理器的实现方和资源管理器的实现方</p>
<span id="more"></span>

<h2 id="1-前言"><a href="#1-前言" class="headerlink" title="1.前言"></a>1.前言</h2><p>Java规范对分布式事务定义了标准的规范Java事务API和Java事务服务，分别是JTA和JTS一个分布式事务必须包括一个事务管理器和多个资源管理器。</p>
<p>资源管理器是任意类型的持久化数据存储；<br>而事务管理器则是承担着所有事务参与单元者的相互通讯的责任。</p>
<p>JTA的规范制定了分布式事务的实现的整套流程框架，定义了各个接口且只有接口，而实现分别交给事务管理器的实现方和资源管理器的实现方</p>
<p>对于资源管理器而言，主要包括数据库连接，JMS等，还有很多了解的不清楚。</p>
<p>对于事务管理器而言，从网上了解主要是应用服务器，包括JBOSS，WEBLOGIC等应用服务器，也就是说事务管理器的实现方是应用服务器，用来管理事务的通讯和协调。<br>对于大多数谈的数据库了解，事务管理器需要从数据库获得XAConnection , XAResource等对象，而这些对象是数据库驱动程序需要提供的，所以如果要实现分布式事务还必须有支持分布式事务的数据库服务器以及数据库驱动程序。</p>
<p>对Mysql而言，在mysql5.0以上的版本已经支持了分布式事务，另外常用的mysql-connector-java-5.1.25-bin.jar也是支持分布式事务的，可以在jar包的com.mysql.jdbc.jdbc2.optional中找到XA对象的实现<br>上面介绍了事务管理器和资源管理器的实现方式，在学习研究过程中发现对于事务管理器，特别强调了tomcat等服务器是不支持的，这句话的意思应该是在tomcat容器内<br>并没有分布式事务管理器的实现对象。而在JBOSS或者WEBLOGIC等商业服务器应该内置了分布式事务管理器的实现对象，应用程序可以通过JNDI方式获取UserTransaction<br>和TransactionManager等分布式事务环境中所需要用到的对象。</p>
<p>通常，应用程序服务器（Application Server）提供了应用程序可以使用的多种服务。在谈到分布式事务时，该服务就称作 XA Resource。当然，在应用程序可以使用 XA Resource 之前，首先要在应用程序服务器中注册和配置 XA Resource。</p>
<p>事务管理器作为管理和协调分布式事务的关键处理中心非常重要，所以应用服务器可以单独只用过事务管理器。</p>
<h2 id="2-在SpringBoot中使用分布式事务"><a href="#2-在SpringBoot中使用分布式事务" class="headerlink" title="2.在SpringBoot中使用分布式事务"></a>2.在SpringBoot中使用分布式事务</h2><p>上面主要是一些基本的概念，在学习研究中总结出来的，可能不太全面，下面主要介绍一下在使用Spring使用分布式事务中的心得，这种做法也是将事务管理器嵌入应用中。</p>
<p>开始准备Spring的时候，Spring官网-SpringBoot文档第38章介绍了Atomikos和Bitronix 等工具，实际上这些工具都是取代应用服务器对事务管理器的支持，负责实现事务管理器对象。由于Atomikos介绍在Bitronix 之前，所以直接使用Atomikos进行测试。</p>
<h3 id="2-1-盲点解释"><a href="#2-1-盲点解释" class="headerlink" title="2.1.盲点解释"></a>2.1.盲点解释</h3><p>要理解 JTA 的实现原理首先需要了解其架构：它包括事务管理器（Transaction Manager）和一个或多个支持 XA 协议的资源管理器 ( Resource Manager ) 两部分， 我们可以将资源管理器看做任意类型的持久化数据存储；事务管理器则承担着所有事务参与单元的协调与控制。 根据所面向对象的不同，我们可以将 JTA 的事务管理器和资源管理器理解为两个方面：面向开发人员的使用接口（事务管理器）和面向服务提供商的实现接口（资源管理器）。其中开发接口的主要部分即为上述示例中引用的 UserTransaction 对象，开发人员通过此接口在信息系统中实现分布式事务；而实现接口则用来规范提供商（如数据库连接提供商）所提供的事务服务，它约定了事务的资源管理功能，使得 JTA 可以在异构事务资源之间执行协同沟通。以数据库为例，IBM 公司提供了实现分布式事务的数据库驱动程序，Oracle 也提供了实现分布式事务的数据库驱动程序， 在同时使用 DB2 和 Oracle 两种数据库连接时， JTA 即可以根据约定的接口协调者两种事务资源从而实现分布式事务。正是基于统一规范的不同实现使得 JTA 可以协调与控制不同数据库或者 JMS 厂商的事务资源，其架构如下图所示：<br><img src="https://img-blog.csdnimg.cn/20181205164907157.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>图 2.1 JTA体系结构</p>
<p>开发人员使用开发人员接口，实现应用程序对全局事务的支持；各提供商（数据库，JMS 等）依据提供商接口的规范提供事务资源管理功能；事务管理器（ TransactionManager ）将应用对分布式事务的使用映射到实际的事务资源并在事务资源间进行协调与控制。 下面，本文将对包括 UserTransaction、Transaction 和 TransactionManager 在内的三个主要接口以及其定义的方法进行介绍。</p>
<p><img src="https://img-blog.csdnimg.cn/20181205164937608.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<ul>
<li>UserTransaction:面向开发人员的接口,开发人员通常只使用此接口实现 JTA 事务管理</li>
<li>Transaction:代表了一个物理意义上的事务</li>
<li>TransactionManager:本身并不承担实际的事务处理功能，它更多的是充当用户接口和实现接口之间的桥梁</li>
<li>UserTransaction 对象不会对事务进行任何控制，所有的事务方法都是通过 TransactionManager 传递到实际的事务资源即 Transaction 对象上</li>
</ul>
<h3 id="2-2-确保mysql开启XA事务支持"><a href="#2-2-确保mysql开启XA事务支持" class="headerlink" title="2.2.确保mysql开启XA事务支持"></a>2.2.确保mysql开启XA事务支持</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SHOW</span> VARIABLES <span class="keyword">LIKE</span> <span class="string">&#x27;%xa%&#x27;</span>  </span><br></pre></td></tr></table></figure>

<p>如果innodb_support_xa的值是ON就说明mysql已经开启对XA事务的支持了。<br>如果不是就执行：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SET</span> innodb_support_xa <span class="operator">=</span> <span class="keyword">ON</span>  </span><br></pre></td></tr></table></figure>

<h3 id="2-3-重要Maven依赖"><a href="#2-3-重要Maven依赖" class="headerlink" title="2.3.重要Maven依赖"></a>2.3.重要Maven依赖</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 数据库连接 --&gt;</span>    </span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span>    </span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>    </span><br><span class="line"><span class="comment">&lt;!-- 分布式事务atomikos --&gt;</span>    </span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-jta-atomikos<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>    </span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>    </span><br><span class="line"><span class="comment">&lt;!-- Druid --&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>druid-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line"><span class="comment">&lt;!-- MyBatis --&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h3 id="2-4-配置Atomikos-Druid-MyBatis"><a href="#2-4-配置Atomikos-Druid-MyBatis" class="headerlink" title="2.4.配置Atomikos, Druid, MyBatis"></a>2.4.配置Atomikos, Druid, MyBatis</h3><p>首先，要使下面的代码配置生效要先确保你在项目工程中引入了spring-boot-starter-jta-atomikos, druid-spring-boot-starter这两个依赖。<br>第二，SpringBoot会自动配置Atomikos的事务管理配置，无需做其它的配置。</p>
<h4 id="2-4-1-application-properties"><a href="#2-4-1-application-properties" class="headerlink" title="2.4.1.application.properties"></a>2.4.1.application.properties</h4><p>第15-19行代码表示实现javax.sql.XADataSource接口的com.alibaba.druid.pool.xa.DruidXADataSource的特有属性, 并不是Atomikos的属性.</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">spring.application.name=learn-jta-atomikos</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开启下划线-驼峰命名转换</span></span><br><span class="line"><span class="string">mybatis.configuration.map-underscore-to-camel-case=true</span></span><br><span class="line"></span><br><span class="line"><span class="string">spring.aop.proxy-target-class=true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## jta相关参数配置</span></span><br><span class="line"><span class="comment"># 如果你在JTA环境中，并且仍然希望使用本地事务，你可以设置spring.jta.enabled属性为false以禁用JTA自动配置。</span></span><br><span class="line"><span class="string">spring.jta.enabled=true</span></span><br><span class="line"><span class="comment"># 必须配置唯一的资源名</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.unique-resource-name=jta-personal</span></span><br><span class="line"><span class="comment"># 配置Druid的属性 https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-data-source-class-name=com.alibaba.druid.pool.xa.DruidXADataSource</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.url=jdbc:mysql://localhost:3306/personal?characterEncoding=utf-8&amp;useSSL=false&amp;allowMultiQueries=true</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.username=root</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.password=123456</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.filters=slf4j,stat,wall,config</span></span><br><span class="line"><span class="comment">#spring.jta.atomikos.datasource.one.xa-properties.connectionProperties=config.decrypt=true;config.decrypt.key=$&#123;druid.publickey&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.unique-resource-name=jta-book</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.max-pool-size=8</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-data-source-class-name=com.alibaba.druid.pool.xa.DruidXADataSource</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.url=jdbc:mysql://localhost:3306/secondary?characterEncoding=utf-8&amp;useSSL=false&amp;&amp;allowMultiQueries=true</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.username=root</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.password=123456</span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.filters=slf4j,stat,wall,config</span></span><br><span class="line"><span class="comment">#spring.jta.atomikos.datasource.two.xa-properties.connectionProperties=config.decrypt=true;config.decrypt.key=$&#123;druid.publickey&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## Druid监控设置</span></span><br><span class="line"><span class="string">spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</span></span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*</span></span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.reset-enable=true</span></span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.login-username=admin</span></span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.login-password=admin</span></span><br><span class="line"><span class="string">spring.datasource.druid.aop-patterns=com.example.atomikos.service.*</span></span><br></pre></td></tr></table></figure>

<h4 id="2-4-2-配置Atomikos数据源与MyBatis集成"><a href="#2-4-2-配置Atomikos数据源与MyBatis集成" class="headerlink" title="2.4.2.配置Atomikos数据源与MyBatis集成"></a>2.4.2.配置Atomikos数据源与MyBatis集成</h4><p>这里只给出默认数据源的Atomikos与MyBatis的集成，其余的数据源的配置与它大同小异(见第2.5章实例)。<br>注意！第18行代码，这里指定com.example.atomikos.dao.one这个包路径下Mapper接口的MyBatis的会话工厂，不同的数据源指定不同的会话工厂！！！<br>然后在使用dao层的时候，正常使用即可，详细代码见(见第2.5章实例)。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.atomikos.jdbc.AtomikosDataSourceBean;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;</span><br><span class="line"><span class="keyword">import</span> org.mybatis.spring.SqlSessionFactoryBean;</span><br><span class="line"><span class="keyword">import</span> org.mybatis.spring.annotation.MapperScan;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Primary;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.sql.DataSource;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 设置JTA(Atomikos)数据源，Mybatis</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@MapperScan(basePackages = &quot;com.example.atomikos.dao.one&quot;, sqlSessionFactoryRef = &quot;oneSqlSessionFactory&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OneDatabaseConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 设置JTA(Atomikos)数据源</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> AtomikosDataSourceBean&#125;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Primary</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="meta">@ConfigurationProperties(prefix = &quot;spring.jta.atomikos.datasource.one&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">oneDataSource</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AtomikosDataSourceBean();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 设置Mybatis的会话工厂类</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> dataSource JTA(Atomikos)数据源</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> SqlSessionFactoryBean#getObject()&#125;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Primary</span></span><br><span class="line">    <span class="meta">@Bean(name = &quot;oneSqlSessionFactory&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> SqlSessionFactory <span class="title">oneSqlSessionFactory</span><span class="params">(DataSource dataSource)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        SqlSessionFactoryBean bean = <span class="keyword">new</span> SqlSessionFactoryBean();</span><br><span class="line">        bean.setDataSource(dataSource);</span><br><span class="line">        <span class="keyword">return</span> bean.getObject();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-4-3-配置声明式事务-tx-aop"><a href="#2-4-3-配置声明式事务-tx-aop" class="headerlink" title="2.4.3.配置声明式事务(tx+aop)"></a>2.4.3.配置声明式事务(tx+aop)</h4><p>Java配置声明式事务AOP</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.aop.Advisor;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.aop.aspectj.AspectJExpressionPointcut;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.aop.support.DefaultPointcutAdvisor;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.PlatformTransactionManager;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.TransactionDefinition;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.interceptor.*;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.util.Collections;  </span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;  </span><br><span class="line"><span class="keyword">import</span> java.util.Map;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 配置声明式事务 切面拦截 </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Configuration</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransactionConfig</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TX_METHOD_TIMEOUT = <span class="number">5</span>;  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String AOP_POINTCUT_EXPRESSION = <span class="string">&quot;execution (* com.example.atomikos.service.*.*(..))&quot;</span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    <span class="keyword">private</span> PlatformTransactionManager transactionManager;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Bean</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> TransactionInterceptor <span class="title">txAdvice</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        NameMatchTransactionAttributeSource source = <span class="keyword">new</span> NameMatchTransactionAttributeSource();  </span><br><span class="line">  </span><br><span class="line">        <span class="comment">/* 只读事务，不做更新操作 */</span>  </span><br><span class="line">        RuleBasedTransactionAttribute readOnlyTx = <span class="keyword">new</span> RuleBasedTransactionAttribute();  </span><br><span class="line">        readOnlyTx.setReadOnly(<span class="keyword">true</span>);  </span><br><span class="line">        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);  </span><br><span class="line">  </span><br><span class="line">        <span class="comment">/* 当前存在事务就使用当前事务，当前不存在事务就创建一个新的事务 */</span>  </span><br><span class="line">        RuleBasedTransactionAttribute requiredTx = <span class="keyword">new</span> RuleBasedTransactionAttribute();  </span><br><span class="line">        requiredTx.setRollbackRules(Collections.singletonList(<span class="keyword">new</span> RollbackRuleAttribute(Exception.class)));  </span><br><span class="line">        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);  </span><br><span class="line">        requiredTx.setTimeout(TX_METHOD_TIMEOUT);  </span><br><span class="line">        Map&lt;String, TransactionAttribute&gt; txMap = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">10</span>);  </span><br><span class="line">  </span><br><span class="line">        txMap.put(<span class="string">&quot;add*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;save*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;insert*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;update*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;delete*&quot;</span>, requiredTx);  </span><br><span class="line">  </span><br><span class="line">        txMap.put(<span class="string">&quot;get*&quot;</span>, readOnlyTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;query*&quot;</span>, readOnlyTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;list*&quot;</span>, readOnlyTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;find*&quot;</span>, readOnlyTx);  </span><br><span class="line">        source.setNameMap(txMap);  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> TransactionInterceptor(transactionManager, source);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 切点 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Bean</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> Advisor <span class="title">txAdviceAdvisor</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        AspectJExpressionPointcut pointcut = <span class="keyword">new</span> AspectJExpressionPointcut();  </span><br><span class="line">        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> DefaultPointcutAdvisor(pointcut, txAdvice());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>等同于下面的Spring XML配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span>  </span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>  </span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:tx</span>=<span class="string">&quot;http://www.springframework.org/schema/tx&quot;</span>  </span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:aop</span>=<span class="string">&quot;http://www.springframework.org/schema/aop&quot;</span>  </span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;  </span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  </span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  </span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd&quot;</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">&lt;!-- 配置事务传播特性 --&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">&quot;txAdvice&quot;</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">tx:attributes</span>&gt;</span>  </span><br><span class="line">            <span class="comment">&lt;!--  </span></span><br><span class="line"><span class="comment">                            name        ：绑定事务的方法名，可以使用通配符，可以配置多个。  </span></span><br><span class="line"><span class="comment">                            propagation ：传播行为  </span></span><br><span class="line"><span class="comment">                            isolation   ：隔离级别  </span></span><br><span class="line"><span class="comment">                            read-only   ：是否只读  </span></span><br><span class="line"><span class="comment">                            timeout     ：超时信息  </span></span><br><span class="line"><span class="comment">                            rollback-for：发生哪些异常回滚.  </span></span><br><span class="line"><span class="comment">                            no-rollback-for：发生哪些异常不回滚.  </span></span><br><span class="line"><span class="comment">                        --&gt;</span>  </span><br><span class="line">            <span class="comment">&lt;!-- 哪些方法加事务 --&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;query*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;SUPPORTS&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;true&quot;</span>/&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;get*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;SUPPORTS&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;true&quot;</span>/&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;select*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;SUPPORTS&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;true&quot;</span>/&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;list*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;SUPPORTS&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;true&quot;</span>/&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;save*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">rollback-for</span>=<span class="string">&quot;Exception&quot;</span>/&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;update*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">rollback-for</span>=<span class="string">&quot;Exception&quot;</span>/&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;delete*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">rollback-for</span>=<span class="string">&quot;Exception&quot;</span>/&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;add*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">rollback-for</span>=<span class="string">&quot;Exception&quot;</span>/&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">tx:attributes</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">tx:advice</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:config</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 注意：如果是自己编写的切面，使用&lt;aop:aspect&gt;标签，如果是系统制作的，使用&lt;aop:advisor&gt;标签。 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;execution (* com.example.atomikos.service.*.*(..))&quot;</span> <span class="attr">order</span>=<span class="string">&quot;0&quot;</span> /&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h3 id="2-5-实例"><a href="#2-5-实例" class="headerlink" title="2.5.实例"></a>2.5.实例</h3><h4 id="2-5-1-项目结构"><a href="#2-5-1-项目结构" class="headerlink" title="2.5.1.项目结构"></a>2.5.1.项目结构</h4><p><img src="https://img-blog.csdnimg.cn/2018120517000116.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="项目工程结构"></p>
<h4 id="2-5-2-数据库"><a href="#2-5-2-数据库" class="headerlink" title="2.5.2.数据库"></a>2.5.2.数据库</h4><p>personal.sql</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> article  </span><br><span class="line">(  </span><br><span class="line">  id <span class="type">bigint</span> unsigned auto_increment  </span><br><span class="line">    <span class="keyword">primary</span> key,  </span><br><span class="line">  content <span class="type">varchar</span>(<span class="number">255</span>) <span class="keyword">null</span>,  </span><br><span class="line">  title <span class="type">varchar</span>(<span class="number">255</span>) <span class="keyword">null</span>,  </span><br><span class="line">  url <span class="type">varchar</span>(<span class="number">255</span>) <span class="keyword">null</span>  </span><br><span class="line">);  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> <span class="keyword">user</span>  </span><br><span class="line">(  </span><br><span class="line">  id <span class="type">bigint</span> unsigned auto_increment <span class="keyword">primary</span> key,  </span><br><span class="line">  username <span class="type">varchar</span>(<span class="number">255</span>) charset utf8 <span class="keyword">null</span>,  </span><br><span class="line">  password <span class="type">varchar</span>(<span class="number">255</span>) charset utf8 <span class="keyword">null</span>  </span><br><span class="line">);  </span><br></pre></td></tr></table></figure>

<p>secondary.sql</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> book  </span><br><span class="line">(  </span><br><span class="line">  id         <span class="type">bigint</span> unsigned auto_increment <span class="keyword">primary</span> key,  </span><br><span class="line">  name       <span class="type">varchar</span>(<span class="number">255</span>)    <span class="keyword">null</span>,  </span><br><span class="line">  article_id <span class="type">bigint</span> unsigned <span class="keyword">null</span>,  </span><br><span class="line">  user_id    <span class="type">bigint</span> unsigned <span class="keyword">null</span>  </span><br><span class="line">);  </span><br></pre></td></tr></table></figure>

<h4 id="2-5-3-Maven依赖"><a href="#2-5-3-Maven依赖" class="headerlink" title="2.5.3.Maven依赖"></a>2.5.3.Maven依赖</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>  </span></span><br><span class="line"><span class="tag">         <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.example<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>learn-jta-atomikos-SpringBoot<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>learn-jta-atomikos<span class="tag">&lt;/<span class="name">name</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">description</span>&gt;</span>Demo project for Spring Boot<span class="tag">&lt;/<span class="name">description</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">parent</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.5.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">relativePath</span>/&gt;</span> <span class="comment">&lt;!-- lookup parent from repository --&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">parent</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">project.reporting.outputEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.reporting.outputEncoding</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">java.version</span>&gt;</span>1.8<span class="tag">&lt;/<span class="name">java.version</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">properties</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- MyBatis --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span>  </span><br><span class="line">                <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span>  </span><br><span class="line">                    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-logging<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">                    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">                <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 热部署 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-devtools<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 数据库连接 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>druid-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 分布式事务atomikos --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-jta-atomikos<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- tx + aop --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.aspectj<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>aspectjweaver<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.9.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 添加Log4j2 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-log4j2<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 为log4j2添加异步支持 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.lmax<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>disruptor<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 简化代码 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 用于监控与管理 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span>  </span><br><span class="line">                <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span>  </span><br><span class="line">                    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-logging<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">                    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">                <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- WEB --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 配合@ConfigurationProperties编译生成元数据文件(IDEA编辑器的属性提示) --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-configuration-processor<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">        <span class="comment">&lt;!-- 测试 --&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">build</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">plugins</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;<span class="name">plugin</span>&gt;</span>  </span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span>  </span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span>  </span><br><span class="line">            <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">build</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h4 id="2-5-4-配置"><a href="#2-5-4-配置" class="headerlink" title="2.5.4.配置"></a>2.5.4.配置</h4><h5 id="application-properties"><a href="#application-properties" class="headerlink" title="application.properties"></a>application.properties</h5><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">spring.application.name=learn-jta-atomikos</span>  </span><br><span class="line">  </span><br><span class="line"><span class="comment"># 开启下划线-驼峰命名转换  </span></span><br><span class="line"><span class="string">mybatis.configuration.map-underscore-to-camel-case=true</span>  </span><br><span class="line">  </span><br><span class="line"><span class="string">spring.aop.proxy-target-class=true</span>  </span><br><span class="line">  </span><br><span class="line"><span class="comment">## jta相关参数配置  </span></span><br><span class="line"><span class="comment"># 如果你在JTA环境中，并且仍然希望使用本地事务，你可以设置spring.jta.enabled属性为false以禁用JTA自动配置。  </span></span><br><span class="line"><span class="string">spring.jta.enabled=true</span>  </span><br><span class="line"><span class="comment"># 必须配置唯一的资源名  </span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.unique-resource-name=jta-personal</span>  </span><br><span class="line"><span class="comment"># 配置Druid的属性 https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8  </span></span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-data-source-class-name=com.alibaba.druid.pool.xa.DruidXADataSource</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.url=jdbc:mysql://localhost:3306/personal?characterEncoding=utf-8&amp;useSSL=false&amp;allowMultiQueries=true</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.username=root</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.password=123456</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.one.xa-properties.filters=slf4j,stat,wall,config</span>  </span><br><span class="line"><span class="comment">#spring.jta.atomikos.datasource.one.xa-properties.connectionProperties=config.decrypt=true;config.decrypt.key=$&#123;druid.publickey&#125;  </span></span><br><span class="line">  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.unique-resource-name=jta-book</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.max-pool-size=8</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-data-source-class-name=com.alibaba.druid.pool.xa.DruidXADataSource</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.url=jdbc:mysql://localhost:3306/secondary?characterEncoding=utf-8&amp;useSSL=false&amp;&amp;allowMultiQueries=true</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.username=root</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.password=123456</span>  </span><br><span class="line"><span class="string">spring.jta.atomikos.datasource.two.xa-properties.filters=slf4j,stat,wall,config</span>  </span><br><span class="line"><span class="comment">#spring.jta.atomikos.datasource.two.xa-properties.connectionProperties=config.decrypt=true;config.decrypt.key=$&#123;druid.publickey&#125;  </span></span><br><span class="line">  </span><br><span class="line"><span class="comment">## Druid监控设置  </span></span><br><span class="line"><span class="string">spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</span>  </span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*</span>  </span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.reset-enable=true</span>  </span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.login-username=admin</span>  </span><br><span class="line"><span class="string">spring.datasource.druid.stat-view-servlet.login-password=admin</span>  </span><br><span class="line"><span class="string">spring.datasource.druid.aop-patterns=com.example.atomikos.service.*</span>  </span><br></pre></td></tr></table></figure>

<h5 id="OneDatabaseConfig"><a href="#OneDatabaseConfig" class="headerlink" title="OneDatabaseConfig"></a>OneDatabaseConfig</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.config;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.atomikos.jdbc.AtomikosDataSourceBean;  </span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;  </span><br><span class="line"><span class="keyword">import</span> org.mybatis.spring.SqlSessionFactoryBean;  </span><br><span class="line"><span class="keyword">import</span> org.mybatis.spring.annotation.MapperScan;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Primary;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> javax.sql.DataSource;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 设置JTA(Atomikos)数据源，Mybatis </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Configuration</span>  </span><br><span class="line"><span class="meta">@MapperScan(basePackages = &quot;com.example.atomikos.dao.one&quot;, sqlSessionFactoryRef = &quot;oneSqlSessionFactory&quot;)</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OneDatabaseConfig</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 设置JTA(Atomikos)数据源 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> AtomikosDataSourceBean&#125; </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Primary</span>  </span><br><span class="line">    <span class="meta">@Bean</span>  </span><br><span class="line">    <span class="meta">@ConfigurationProperties(prefix = &quot;spring.jta.atomikos.datasource.one&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">oneDataSource</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AtomikosDataSourceBean();  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 设置Mybatis的会话工厂类 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> dataSource JTA(Atomikos)数据源 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> SqlSessionFactoryBean#getObject()&#125; </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Primary</span>  </span><br><span class="line">    <span class="meta">@Bean(name = &quot;oneSqlSessionFactory&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> SqlSessionFactory <span class="title">oneSqlSessionFactory</span><span class="params">(DataSource dataSource)</span> <span class="keyword">throws</span> Exception </span>&#123;  </span><br><span class="line">        SqlSessionFactoryBean bean = <span class="keyword">new</span> SqlSessionFactoryBean();  </span><br><span class="line">        bean.setDataSource(dataSource);  </span><br><span class="line">        <span class="keyword">return</span> bean.getObject();  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h5 id="TwoDatabaseConfig"><a href="#TwoDatabaseConfig" class="headerlink" title="TwoDatabaseConfig"></a>TwoDatabaseConfig</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.config;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.atomikos.jdbc.AtomikosDataSourceBean;  </span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;  </span><br><span class="line"><span class="keyword">import</span> org.mybatis.spring.SqlSessionFactoryBean;  </span><br><span class="line"><span class="keyword">import</span> org.mybatis.spring.annotation.MapperScan;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Qualifier;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> javax.sql.DataSource;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 设置JTA(Atomikos)数据源，Mybatis </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Configuration</span>  </span><br><span class="line"><span class="meta">@MapperScan(basePackages = &quot;com.example.atomikos.dao.two&quot;, sqlSessionFactoryRef = &quot;twoSqlSessionFactory&quot;)</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TwoDatabaseConfig</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 设置JTA(Atomikos)数据源 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> AtomikosDataSourceBean&#125; </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Bean(name = &quot;twoAtomikosDataSource&quot;)</span>  </span><br><span class="line">    <span class="meta">@ConfigurationProperties(prefix = &quot;spring.jta.atomikos.datasource.two&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> DataSource <span class="title">oneDataSource</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AtomikosDataSourceBean();  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 设置Mybatis的会话工厂类 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> dataSource JTA(Atomikos)数据源 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#123;<span class="doctag">@link</span> SqlSessionFactoryBean#getObject()&#125; </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Bean(name = &quot;twoSqlSessionFactory&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> SqlSessionFactory <span class="title">oneSqlSessionFactory</span><span class="params">(<span class="meta">@Qualifier(&quot;twoAtomikosDataSource&quot;)</span> DataSource dataSource)</span> <span class="keyword">throws</span> Exception </span>&#123;  </span><br><span class="line">        SqlSessionFactoryBean bean = <span class="keyword">new</span> SqlSessionFactoryBean();  </span><br><span class="line">        bean.setDataSource(dataSource);  </span><br><span class="line">        <span class="keyword">return</span> bean.getObject();  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h5 id="TransactionConfig"><a href="#TransactionConfig" class="headerlink" title="TransactionConfig"></a>TransactionConfig</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.config;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> org.springframework.aop.Advisor;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.aop.aspectj.AspectJExpressionPointcut;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.aop.support.DefaultPointcutAdvisor;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.PlatformTransactionManager;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.TransactionDefinition;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.interceptor.*;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.util.Collections;  </span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;  </span><br><span class="line"><span class="keyword">import</span> java.util.Map;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 配置声明式事务 切面拦截 </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Configuration</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransactionConfig</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TX_METHOD_TIMEOUT = <span class="number">5</span>;  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String AOP_POINTCUT_EXPRESSION = <span class="string">&quot;execution (* com.example.atomikos.service.*.*(..))&quot;</span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    <span class="keyword">private</span> PlatformTransactionManager transactionManager;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Bean</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> TransactionInterceptor <span class="title">txAdvice</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        NameMatchTransactionAttributeSource source = <span class="keyword">new</span> NameMatchTransactionAttributeSource();  </span><br><span class="line">  </span><br><span class="line">        <span class="comment">/* 只读事务，不做更新操作 */</span>  </span><br><span class="line">        RuleBasedTransactionAttribute readOnlyTx = <span class="keyword">new</span> RuleBasedTransactionAttribute();  </span><br><span class="line">        readOnlyTx.setReadOnly(<span class="keyword">true</span>);  </span><br><span class="line">        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);  </span><br><span class="line">  </span><br><span class="line">        <span class="comment">/* 当前存在事务就使用当前事务，当前不存在事务就创建一个新的事务 */</span>  </span><br><span class="line">        RuleBasedTransactionAttribute requiredTx = <span class="keyword">new</span> RuleBasedTransactionAttribute();  </span><br><span class="line">        requiredTx.setRollbackRules(Collections.singletonList(<span class="keyword">new</span> RollbackRuleAttribute(Exception.class)));  </span><br><span class="line">        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);  </span><br><span class="line">        requiredTx.setTimeout(TX_METHOD_TIMEOUT);  </span><br><span class="line">        Map&lt;String, TransactionAttribute&gt; txMap = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">10</span>);  </span><br><span class="line">  </span><br><span class="line">        txMap.put(<span class="string">&quot;add*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;save*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;insert*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;update*&quot;</span>, requiredTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;delete*&quot;</span>, requiredTx);  </span><br><span class="line">  </span><br><span class="line">        txMap.put(<span class="string">&quot;get*&quot;</span>, readOnlyTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;query*&quot;</span>, readOnlyTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;list*&quot;</span>, readOnlyTx);  </span><br><span class="line">        txMap.put(<span class="string">&quot;find*&quot;</span>, readOnlyTx);  </span><br><span class="line">        source.setNameMap(txMap);  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> TransactionInterceptor(transactionManager, source);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 切点 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Bean</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> Advisor <span class="title">txAdviceAdvisor</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        AspectJExpressionPointcut pointcut = <span class="keyword">new</span> AspectJExpressionPointcut();  </span><br><span class="line">        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> DefaultPointcutAdvisor(pointcut, txAdvice());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h4 id="2-5-5-实体类"><a href="#2-5-5-实体类" class="headerlink" title="2.5.5.实体类"></a>2.5.5.实体类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.entity;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;  </span><br><span class="line"><span class="keyword">import</span> lombok.Data;  </span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 文章 </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Data</span>  </span><br><span class="line"><span class="meta">@NoArgsConstructor</span>  </span><br><span class="line"><span class="meta">@AllArgsConstructor</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArticleDO</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">3971756585655871603L</span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> Long id;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> String title;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> String content;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> String url;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.entity;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;  </span><br><span class="line"><span class="keyword">import</span> lombok.Data;  </span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 书 </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Data</span>  </span><br><span class="line"><span class="meta">@NoArgsConstructor</span>  </span><br><span class="line"><span class="meta">@AllArgsConstructor</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookDO</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">3231762613546697469L</span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> Long id;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> String name;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> Long articleId;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> Long userId;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.entity;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> lombok.Data;  </span><br><span class="line">  </span><br><span class="line"><span class="meta">@Data</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookVo</span> <span class="keyword">extends</span> <span class="title">BookDO</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> UserDO user;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
 <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.entity;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;  </span><br><span class="line"><span class="keyword">import</span> lombok.Data;  </span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 用户 </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Data</span>  </span><br><span class="line"><span class="meta">@NoArgsConstructor</span>  </span><br><span class="line"><span class="meta">@AllArgsConstructor</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserDO</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">469663920369239035L</span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> Long id;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> String username;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> String password;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h4 id="2-5-6-Dao层"><a href="#2-5-6-Dao层" class="headerlink" title="2.5.6.Dao层"></a>2.5.6.Dao层</h4><h5 id="UserDao"><a href="#UserDao" class="headerlink" title="UserDao"></a>UserDao</h5><p>注意包名，UserDao对应的配置为OneDatabaseConfig</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.dao.one;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.UserDO;  </span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.annotations.*;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Repository;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.util.List;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2018/11/30 </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Repository</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserDao</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 根据主键查询一条记录 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Select(&quot;select id, username, password from user where id = #&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function">UserDO <span class="title">get</span><span class="params">(Long id)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 分页列表查询 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> page </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> size </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Select(&quot;select id, username, password from user limit #&#123;page&#125;, #&#123;size&#125;&quot;)</span>  </span><br><span class="line">    <span class="function">List&lt;UserDO&gt; <span class="title">list</span><span class="params">(Integer page, Integer size)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 保存 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> userDO </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 自增主键 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Insert(&quot;insert into user(username, password) values(#&#123;username&#125;, #&#123;password&#125;)&quot;)</span>  </span><br><span class="line">    <span class="meta">@Options(useGeneratedKeys = true, keyColumn = &quot;id&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">save</span><span class="params">(UserDO userDO)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 修改一条记录 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> user </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Update(&quot;update user set username = #&#123;username&#125;, password = #&#123;password&#125; where id = #&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">update</span><span class="params">(UserDO user)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 删除一条记录 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id 主键 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Delete(&quot;delete from user where id = #&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">delete</span><span class="params">(Long id)</span></span>;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h5 id="BookDao"><a href="#BookDao" class="headerlink" title="BookDao"></a>BookDao</h5><p>注意包名，UserDao对应的配置为TwoDatabaseConfig</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.dao.two;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.BookDO;  </span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.annotations.*;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Repository;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.util.List;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2018/11/30 </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Mapper</span>  </span><br><span class="line"><span class="meta">@Repository</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BookDao</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 分页查询 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> page 页码 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> size 每页记录数 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Select(&quot;select id, name, article_id as articleId, user_id as userId from book limit $&#123;page&#125;, $&#123;size&#125;&quot;)</span>  </span><br><span class="line">    <span class="function">List&lt;BookDO&gt; <span class="title">list</span><span class="params">(<span class="meta">@Param(&quot;page&quot;)</span> Integer page, <span class="meta">@Param(&quot;size&quot;)</span> Integer size)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 根据主键查询单条记录 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Select(&quot;select id, name, article_id as articleId, user_id as userId from book where id = #&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function">BookDO <span class="title">get</span><span class="params">(Long id)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 添加一条记录 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> book </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 自增主键 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Insert(&quot;insert into book(name, article_id, user_id) values(#&#123;name&#125;, #&#123;articleId&#125;, #&#123;userId&#125;)&quot;)</span>  </span><br><span class="line">    <span class="meta">@Options(useGeneratedKeys = true, keyProperty = &quot;id&quot;, keyColumn = &quot;id&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">save</span><span class="params">(BookDO book)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 修改一条记录 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> book </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Update(&quot;update book set name = #&#123;name&#125;, article_id = #&#123;articleId&#125;, user_id = #&#123;userId&#125; where id = #&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">update</span><span class="params">(BookDO book)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 删除一条记录 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id 主键 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Delete(&quot;delete from book where id = #&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">delete</span><span class="params">(Long id)</span></span>;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h4 id="2-5-7-Service层"><a href="#2-5-7-Service层" class="headerlink" title="2.5.7.Service层"></a>2.5.7.Service层</h4><h5 id="Bookservice"><a href="#Bookservice" class="headerlink" title="Bookservice"></a>Bookservice</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.service;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.BookDO;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.UserDO;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.util.List;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 主要目的是测试分布式事务 </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BookService</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 保存 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> book </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> user </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function">BookDO <span class="title">save</span><span class="params">(BookDO book, UserDO user)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 单条查询 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function">BookDO <span class="title">get</span><span class="params">(Long id)</span></span>;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 分页查询 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> page </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> size </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function">List&lt;BookDO&gt; <span class="title">list</span><span class="params">(Integer page, Integer size)</span></span>;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h5 id="BookServiceImpl"><a href="#BookServiceImpl" class="headerlink" title="BookServiceImpl"></a>BookServiceImpl</h5><p>请注意，其中有些代码故意抛出异常是为了测试的目的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.service.impl;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.dao.one.UserDao;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.dao.two.BookDao;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.BookDO;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.UserDO;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.service.BookService;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.annotation.Transactional;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.util.List;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@Service</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookServiceImpl</span> <span class="keyword">implements</span> <span class="title">BookService</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    <span class="keyword">private</span> BookDao bookDao;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    <span class="keyword">private</span> UserDao userDao;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 保存书本和文章, 使用声明式事务(tx+aop形式) </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> book &#123;<span class="doctag">@link</span> BookDO&#125; </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> user &#123;<span class="doctag">@link</span> UserDO&#125; </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Override</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> BookDO <span class="title">save</span><span class="params">(BookDO book, UserDO user)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">int</span> userSave = userDao.save(user);  </span><br><span class="line">        <span class="keyword">if</span> (userSave == <span class="number">0</span>) &#123;  </span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;  </span><br><span class="line">        &#125;  </span><br><span class="line">        book.setUserId(user.getId());  </span><br><span class="line">        <span class="keyword">int</span> bookSave = bookDao.save(book);  </span><br><span class="line">        <span class="keyword">if</span> (bookSave == <span class="number">0</span>) &#123;  </span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;  </span><br><span class="line">        &#125;  </span><br><span class="line"><span class="comment">//        throw new RuntimeException(&quot;测试分布式事务(tx+aop形式)&quot;);  </span></span><br><span class="line">        <span class="keyword">return</span> book;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 单条查询 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Override</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> BookDO <span class="title">get</span><span class="params">(Long id)</span> </span>&#123;  </span><br><span class="line">        BookDO book = bookDao.get(id);  </span><br><span class="line">        UserDO user = userDao.get(book.getUserId());  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> BookDO(book.getId(), book.getName(), book.getArticleId(), user.getId());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 分页查询 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> page </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> size </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Override</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;BookDO&gt; <span class="title">list</span><span class="params">(Integer page, Integer size)</span> </span>&#123;  </span><br><span class="line">        page = (page &lt; <span class="number">1</span> ? <span class="number">0</span> : page - <span class="number">1</span>) * size;  </span><br><span class="line">        <span class="keyword">return</span> bookDao.list(page, size);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 修改书本和文章, 使用声明式事务(注解形式) </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> book </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> user </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Transactional(rollbackFor = Exception.class)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> BookDO <span class="title">update</span><span class="params">(BookDO book, UserDO user)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">int</span> bookUpdate = bookDao.update(book);  </span><br><span class="line">        <span class="keyword">if</span> (bookUpdate != <span class="number">1</span>) &#123;  </span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">int</span> userUpdate = userDao.update(user);  </span><br><span class="line">        <span class="keyword">if</span> (userUpdate != <span class="number">1</span>) &#123;  </span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">&quot;测试分布式事务(注解形式)&quot;</span>);  </span><br><span class="line"><span class="comment">//        return book;  </span></span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 删除书本和文章 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">delete</span><span class="params">(Long id)</span> </span>&#123;  </span><br><span class="line">        BookDO book = bookDao.get(id);  </span><br><span class="line">        System.err.println(book);  </span><br><span class="line">        Long userId = book.getUserId();  </span><br><span class="line">        <span class="keyword">int</span> userDelete = userDao.delete(userId);  </span><br><span class="line">        <span class="keyword">if</span> (userDelete != <span class="number">1</span>) &#123;  </span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">int</span> bookDelete = bookDao.delete(id);  </span><br><span class="line">        <span class="keyword">if</span> (bookDelete != <span class="number">1</span>) &#123;  </span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">&quot;测试没有添加分布式事务管理)&quot;</span>);  </span><br><span class="line"><span class="comment">//        return 1;  </span></span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h4 id="2-5-8-Controller层"><a href="#2-5-8-Controller层" class="headerlink" title="2.5.8.Controller层"></a>2.5.8.Controller层</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.controller;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.BookDO;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.BookVo;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.service.BookService;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.service.impl.BookServiceImpl;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.*;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> java.util.List;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@RestController</span>  </span><br><span class="line"><span class="meta">@RequestMapping(&quot;/books&quot;)</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookController</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    <span class="keyword">private</span> BookService bookService;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@GetMapping</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;BookDO&gt; <span class="title">list</span><span class="params">(  </span></span></span><br><span class="line"><span class="function"><span class="params">            <span class="meta">@RequestParam(defaultValue = &quot;1&quot;)</span> Integer page,  </span></span></span><br><span class="line"><span class="function"><span class="params">            <span class="meta">@RequestParam(defaultValue = &quot;10&quot;)</span> Integer size)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> bookService.list(page, size);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@GetMapping(&quot;/&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> BookDO <span class="title">get</span><span class="params">(<span class="meta">@PathVariable</span> Long id)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> bookService.get(id);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@PostMapping</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> BookDO <span class="title">save</span><span class="params">(<span class="meta">@RequestBody</span> BookVo book)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> bookService.save(book, book.getUser());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@PutMapping</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> BookDO <span class="title">update</span><span class="params">(<span class="meta">@RequestBody</span> BookVo book)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> ((BookServiceImpl) bookService).update(book, book.getUser());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@DeleteMapping(&quot;/&#123;id&#125;&quot;)</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">delete</span><span class="params">(<span class="meta">@PathVariable</span> Long id)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> ((BookServiceImpl) bookService).delete(id);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h4 id="2-5-9-单元测试"><a href="#2-5-9-单元测试" class="headerlink" title="2.5.9.单元测试"></a>2.5.9.单元测试</h4><h5 id="BookServiceImplTest"><a href="#BookServiceImplTest" class="headerlink" title="BookServiceImplTest"></a>BookServiceImplTest</h5><p>由于故意抛出异常，故单元测试失败，查看数据库，数据库中数据保持原样</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.service.impl;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.BookDO;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.UserDO;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.service.BookService;  </span><br><span class="line"><span class="keyword">import</span> org.junit.Assert;  </span><br><span class="line"><span class="keyword">import</span> org.junit.Test;  </span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit4.SpringRunner;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 测试分布式事务：切面拦截形式, 注解式 </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@RunWith(SpringRunner.class)</span>  </span><br><span class="line"><span class="meta">@SpringBootTest</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookServiceImplTest</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    BookService bookService;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 测试分布式事务(切面拦截形式) </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Test</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        BookDO book = <span class="keyword">new</span> BookDO();  </span><br><span class="line">        book.setName(<span class="string">&quot;Book Name - 001&quot;</span>);  </span><br><span class="line">        book.setArticleId(<span class="number">69L</span>);  </span><br><span class="line">  </span><br><span class="line">        UserDO user = <span class="keyword">new</span> UserDO();  </span><br><span class="line">        user.setUsername(<span class="string">&quot;username - 001&quot;</span>);  </span><br><span class="line">        user.setPassword(<span class="string">&quot;password - 001&quot;</span>);  </span><br><span class="line">        BookDO bookDO = bookService.save(book, user);  </span><br><span class="line">        System.out.println(bookDO);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 测试分布式事务(注解式) </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Test</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        BookDO book = <span class="keyword">new</span> BookDO();  </span><br><span class="line">        book.setId(<span class="number">10L</span>);  </span><br><span class="line">        book.setName(<span class="string">&quot;Book Name - 002&quot;</span>);  </span><br><span class="line">        book.setArticleId(<span class="number">69L</span>);  </span><br><span class="line">  </span><br><span class="line">        UserDO user = <span class="keyword">new</span> UserDO();  </span><br><span class="line">        user.setId(<span class="number">18L</span>);  </span><br><span class="line">        user.setUsername(<span class="string">&quot;username - 002&quot;</span>);  </span><br><span class="line">        user.setPassword(<span class="string">&quot;password - 002&quot;</span>);  </span><br><span class="line">  </span><br><span class="line">        ((BookServiceImpl)bookService).update(book, user);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 没有事务管理 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Test</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delete</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">int</span> delete = ((BookServiceImpl) bookService).delete(<span class="number">11L</span>);  </span><br><span class="line">        Assert.assertEquals(<span class="number">1</span>, delete);  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h5 id="BookControllerTest"><a href="#BookControllerTest" class="headerlink" title="BookControllerTest"></a>BookControllerTest</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.atomikos.controller;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.dao.one.UserDao;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.BookVo;  </span><br><span class="line"><span class="keyword">import</span> com.example.atomikos.entity.UserDO;  </span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.databind.ObjectMapper;  </span><br><span class="line"><span class="keyword">import</span> org.junit.Before;  </span><br><span class="line"><span class="keyword">import</span> org.junit.Test;  </span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.http.MediaType;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit4.SpringRunner;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.test.web.servlet.MockMvc;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.test.web.servlet.request.MockMvcRequestBuilders;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.test.web.servlet.setup.MockMvcBuilders;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.web.context.WebApplicationContext;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.hamcrest.Matchers.is;  </span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;  </span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;  </span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;  </span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;  </span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 测试分布式事务 </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="meta">@RunWith(SpringRunner.class)</span>  </span><br><span class="line"><span class="meta">@SpringBootTest</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookControllerTest</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> MockMvc mockMvc;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">private</span> ObjectMapper objectMapper = <span class="keyword">new</span> ObjectMapper();  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    <span class="keyword">private</span> UserDao userDao;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Autowired</span>  </span><br><span class="line">    <span class="keyword">private</span> WebApplicationContext context;  </span><br><span class="line">  </span><br><span class="line">    <span class="meta">@Before</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUp</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">this</span>.mockMvc = MockMvcBuilders.webAppContextSetup(<span class="keyword">this</span>.context).build();  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 申明式 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Test</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;  </span><br><span class="line">        UserDO user = <span class="keyword">new</span> UserDO();  </span><br><span class="line">        user.setUsername(<span class="string">&quot;username - 002&quot;</span>);  </span><br><span class="line">        user.setPassword(<span class="string">&quot;password - 002&quot;</span>);  </span><br><span class="line">  </span><br><span class="line">        BookVo book = <span class="keyword">new</span> BookVo();  </span><br><span class="line">        book.setName(<span class="string">&quot;Book Name - 002&quot;</span>);  </span><br><span class="line">        book.setArticleId(<span class="number">69L</span>);  </span><br><span class="line">        book.setUser(user);  </span><br><span class="line">        String json = objectMapper.writeValueAsString(book);  </span><br><span class="line">        <span class="keyword">this</span>.mockMvc.perform(  </span><br><span class="line">                post(<span class="string">&quot;/books&quot;</span>)  </span><br><span class="line">                        .contentType(MediaType.APPLICATION_JSON_UTF8)  </span><br><span class="line">                        .content(json))  </span><br><span class="line">                .andExpect(status().isOk())  </span><br><span class="line">                .andExpect(jsonPath(<span class="string">&quot;$.name&quot;</span>, is(<span class="string">&quot;Book Name - 002&quot;</span>)))  </span><br><span class="line">                .andExpect(jsonPath(<span class="string">&quot;$.articleId&quot;</span>, is(<span class="number">69</span>)))  </span><br><span class="line">                .andDo(print());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 注解式 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Test</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;  </span><br><span class="line">        UserDO user = userDao.get(<span class="number">3L</span>);  </span><br><span class="line">        <span class="keyword">assert</span> user != <span class="keyword">null</span>;  </span><br><span class="line">        user.setUsername(<span class="string">&quot;username - 003&quot;</span>);  </span><br><span class="line">        user.setPassword(<span class="string">&quot;password - 003&quot;</span>);  </span><br><span class="line">  </span><br><span class="line">        BookVo book = <span class="keyword">new</span> BookVo();  </span><br><span class="line">        book.setId(<span class="number">3L</span>);  </span><br><span class="line">        book.setName(<span class="string">&quot;Book Name - 003&quot;</span>);  </span><br><span class="line">        book.setArticleId(<span class="number">69L</span>);  </span><br><span class="line">        book.setUser(user);  </span><br><span class="line">  </span><br><span class="line">        String json = objectMapper.writeValueAsString(book);  </span><br><span class="line">        <span class="keyword">this</span>.mockMvc.perform(  </span><br><span class="line">                put(<span class="string">&quot;/books&quot;</span>)  </span><br><span class="line">                        .contentType(MediaType.APPLICATION_JSON_UTF8)  </span><br><span class="line">                        .content(json))  </span><br><span class="line">                .andExpect(status().isOk())  </span><br><span class="line">                .andExpect(jsonPath(<span class="string">&quot;$.name&quot;</span>, is(<span class="string">&quot;Book Name - 003&quot;</span>)))  </span><br><span class="line">                .andExpect(jsonPath(<span class="string">&quot;$.articleId&quot;</span>, is(<span class="number">69</span>)))  </span><br><span class="line">                .andDo(print());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 没有事务管理 </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="meta">@Test</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delete</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;  </span><br><span class="line">        <span class="keyword">this</span>.mockMvc.perform(  </span><br><span class="line">                MockMvcRequestBuilders.delete(<span class="string">&quot;/books/4&quot;</span>))  </span><br><span class="line">                .andExpect(status().isOk())  </span><br><span class="line">                .andDo(print());  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h2 id="3-引用"><a href="#3-引用" class="headerlink" title="3.引用"></a>3.引用</h2><ul>
<li><a target="_blank" rel="noopener" href="http://www.thedevpiece.com/configuring-multiple-datasources-using-springboot-and-atomikos/">http://www.thedevpiece.com/configuring-multiple-datasources-using-springboot-and-atomikos/</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/a510835147/article/details/75675311">https://blog.csdn.net/a510835147/article/details/75675311</a></li>
<li><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/0dde641295af">https://www.jianshu.com/p/0dde641295af</a></li>
<li><a target="_blank" rel="noopener" href="http://www.importnew.com/15812.html">http://www.importnew.com/15812.html</a></li>
<li><a target="_blank" rel="noopener" href="https://www.ibm.com/developerworks/cn/java/j-lo-jta/">https://www.ibm.com/developerworks/cn/java/j-lo-jta/</a></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/" rel="tag"># 分布式事务</a>
              <a href="/tags/spring-boot/" rel="tag"># spring-boot</a>
              <a href="/tags/atomikos/" rel="tag"># atomikos</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2019/05/06/spring-boot-starter%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E5%85%A5%E9%97%A8/" rel="prev" title="spring-boot-starter单元测试入门">
                  <i class="fa fa-chevron-left"></i> spring-boot-starter单元测试入门
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2019/05/06/spring-boot%E4%B8%AD%E6%B3%A8%E8%A7%A3-EnableAutoConfiguration%E7%9A%84%E8%A7%A3%E6%9E%90/" rel="next" title="spring-boot中注解@EnableAutoConfiguration的解析">
                  spring-boot中注解@EnableAutoConfiguration的解析 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
